package com.ruoyi.service;

import com.ruoyi.common.core.service.BaseServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.entity.SysForm;
import com.ruoyi.mapper.SysFormMapper;
import com.ruoyi.vo.FlowFormSaveVO;
import org.apache.commons.io.IOUtils;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import com.ruoyi.vo.FlowSaveXmlVO;

/**
 * 流程表单Service业务层处理
 * 
 * @author XuanXuan Xuan
 *
 */
@Service
public class SysFormServiceImpl extends BaseServiceImpl<SysFormMapper, SysForm>
{
    @Autowired
    private SysFormMapper sysFormMapper;


    /**
     * 查询流程表单
     * 
     * @param formId 流程表单ID
     * @return 流程表单
     */

    public SysForm selectSysFormById(Long formId)
    {
        return sysFormMapper.selectSysFormById(formId);
    }

    public FlowFormSaveVO getFormFlowInfo(Long formId) throws IOException {
        FlowFormSaveVO flowFormSaveVo = new FlowFormSaveVO();
        SysForm sysForm = sysFormMapper.selectSysFormById(formId);
        flowFormSaveVo.setSysForm(sysForm);

        if(sysForm.getDeployId() != null){
            Deployment deployment = SpringUtils.getBean(RepositoryService.class).createDeploymentQuery().deploymentId(sysForm.getDeployId()).singleResult();
            ProcessDefinition definition = SpringUtils.getBean(RepositoryService.class).createProcessDefinitionQuery().deploymentId(sysForm.getDeployId()).singleResult();
            InputStream inputStream = SpringUtils.getBean(RepositoryService.class).getResourceAsStream(definition.getDeploymentId(), definition.getResourceName());
            String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());

            FlowSaveXmlVO flowSaveXmlVo = new FlowSaveXmlVO();
            flowSaveXmlVo.setXml(result);
            flowSaveXmlVo.setName(deployment.getName());
            flowFormSaveVo.setFlowSaveXmlVo(flowSaveXmlVo);
        }
        return flowFormSaveVo;
    }

    /**
     * 查询流程表单列表
     * 
     * @param sysForm 流程表单
     * @return 流程表单
     */
    
    public List<SysForm> selectSysFormList(SysForm sysForm)
    {
        return sysFormMapper.selectSysFormList(sysForm);
    }

    /**
     * 新增流程表单
     * 
     * @param sysForm 流程表单
     * @return 结果
     */

    public SysForm insertSysForm(SysForm sysForm)
    {
        if(sysForm.getFormId() == null){
            sysForm.setCreateTime(DateUtils.getNowDate());
            sysForm.setState("1");
        }
        //sysForm.setSearchValue(sysForm.getFormName());
        this.saveOrUpdate(sysForm);
        return sysForm;
    }

    /**
     * 修改流程表单
     * 
     * @param sysForm 流程表单
     * @return 结果
     */
    
    public int updateSysForm(SysForm sysForm)
    {
        sysForm.setUpdateTime(DateUtils.getNowDate());
        return sysFormMapper.updateSysForm(sysForm);
    }

    /**
     * 批量删除流程表单
     * 
     * @param formIds 需要删除的流程表单ID
     * @return 结果
     */
    
    public int deleteSysFormByIds(Long[] formIds)
    {
        return sysFormMapper.deleteSysFormByIds(formIds);
    }

    /**
     * 删除流程表单信息
     * 
     * @param formId 流程表单ID
     * @return 结果
     */
    
    public int deleteSysFormById(Long formId)
    {
        return sysFormMapper.deleteSysFormById(formId);
    }
}
